package mall.service;

import mall.core.jdbc.Page;
import mall.entity.User;
import mall.repository.UserRepository;

import java.util.List;

public class UserService {
    private final UserRepository userRepository = new UserRepository();

    public User login(String username, String password) {
        return userRepository.selectByUsernamePassword(username, password);
    }

    public void deleteById(int id) {
        userRepository.deleteById(id);
    }

    public void add(User user) {
        userRepository.insert(user);
    }

    public void update(User user) {
        userRepository.update(user);
    }

    public User getById(int id) {
        return userRepository.selectById(id);
    }

    public List<User> list(User user) {
        return userRepository.selectList(user);
    }

    public List<User> list() {
        return userRepository.selectList();
    }

    public User getByUsername(String username) {
        return userRepository.selectByUsername(username);

    }

    public Page<User> page(Page<User> page, User user) {
        Integer total = userRepository.count(user);
        List<User> list = userRepository.selectList(user, page.getOffset(), page.getPageSize());
        page.setTotal(total);
        page.setList(list);
        return page;
    }
}
